package com.jyw.custom.huaxi.mos.service.impl;

import com.google.common.collect.Sets;
import com.jyw.common.core.domain.entity.SysUser;
import com.jyw.common.core.domain.model.LoginUser;
import com.jyw.common.enums.UserStatus;
import com.jyw.common.exception.ServiceException;
import com.jyw.common.utils.spring.SpringUtils;
import com.jyw.custom.huaxi.mos.service.IHuaxiMosService;
import com.jyw.custom.huaxi.mos.service.IHuaxiMosUserDetailsService;
import com.jyw.system.domain.SysUserDeptRole;
import com.jyw.system.service.ISysUserDeptRoleService;
import com.jyw.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 华西MOS定制登录废弃
 */
@Service
@Slf4j
@Deprecated
public class HuaxiMosUserDetailsServiceImpl implements IHuaxiMosUserDetailsService {

    @Resource
    private ISysUserService userService;

    @Resource
    private ISysUserDeptRoleService userDeptRoleService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //员工工号
        String employeeId = null;
        //查询用户code
        employeeId = SpringUtils.getBean(IHuaxiMosService.class).getAuthUser(username);
        if(ObjectUtils.isEmpty(employeeId)){
            //调用企业微信授权
        }
        SysUser user = userService.selectUserByUserName(employeeId);

        if(ObjectUtils.isEmpty(user)){
            log.info("用户不存在");
            throw new ServiceException("工号:"+employeeId+"不存在");
        } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
            log.info("登录用户:{}已被删除",employeeId);
            throw new ServiceException("对不起,您的账号:"+employeeId+"已被删除");
        } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            log.info("登录用户:{},已被停用",employeeId);
            throw new ServiceException("对不起,您的账号:"+employeeId+"已停用");
        }
        return null;

//        SysUserDeptRole userDeptRole = userDeptRoleService.getUserDefault(user.getUserId());
//        if(ObjectUtils.isEmpty(userDeptRole)){
//            log.info("登录用户:{},没有默认角色");
//            throw new ServiceException("对不起,您的账号:"+employeeId+"没有默认角色");
//        }
//        return new LoginUser(user.getUserId(), userDeptRole.getDeptId(),userDeptRole.getRoleId(),userDeptRole.getId(), user, Sets.newHashSet("*:*:*"));
    }
}
